﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class web_Controls_ctrl_KIOS_td : System.Web.UI.UserControl
{
    Common _common = null;
    public static string sql_where = "";
    public static string _type = "";
    public static string _id = "";
	Common comm=new Common();
    protected void Page_Load(object sender, EventArgs e)
    {
        _common = new Common();
        if (!Page.IsPostBack)
        {
            sql_where = "";
            if (Request.Params["type"] != null)
            {
                _type = Request.Params["type"].ToString();
                div_list_by_cat.Visible = true;
                if (_type.Trim().Equals("1"))
                {
                    lbl_title_fnc.Text = "DANH SÁCH DOANH NGHIỆP TUYỂN DỤNG";
                    LoadDoanhnghiep();
                    if (Request.Params["id"] != null)
                    {
                        _id = _common.TextFormat(Request.Params["id"].ToString());
                        LoadTuyendungByNhatuyendung(_id);
                    }
                }
                if (_type.Trim().Equals("2"))
                {
                    lbl_title_fnc.Text = "DANH SÁCH NGÀNH NGHỀ TUYỂN DỤNG";
                    LoadNganhtuyendung();
                    if (Request.Params["id"] != null)
                    {
                        _id = _common.TextFormat(Request.Params["id"].ToString());
                        LoadTuyendungByNganhtuyendung(_id);
                    }
                }
                if (_type.Trim().Equals("3"))
                {
                    lbl_title_fnc.Text = "DANH SÁCH CHỨC DANH TUYỂN DỤNG";
                    LoadChucdanhTuyendung();
                    if (Request.Params["id"] != null)
                    {
                        _id = _common.TextFormat(Request.Params["id"].ToString());
                        LoadTuyendungByChucdanh(_id);
                    }
                }
            }
            else
            {
				lbl_title_fnc.Text+="Viec lam Binh Duong";
                _type = "";
                LoadTuyendung();
            }
        }
    }
    void LoadTuyendung()
    {
        DataTable totalrow = null;
        if (sql_where.Trim().Equals(""))
        {
            totalrow = _common.getDataTable("select ma_ho_so from ViecLamTrong_ho_so_tuyen_dung where kich_hoat=1");
        }
        else { totalrow = _common.getDataTable("select A.ma_ho_so from ViecLamTrong_ho_so_tuyen_dung as A where A.kich_hoat=1 and " + sql_where); }
        pagination1.ItemCount = totalrow.Rows.Count;
        lbl_total.Text = totalrow.Rows.Count.ToString();
        SqlParameter[] pa = new SqlParameter[7];

        pa[0] = new SqlParameter("@Tables", " ViecLamTrong_ho_so_tuyen_dung as A ");
        pa[1] = new SqlParameter("@PK", "A.ma_ho_so");
        pa[2] = new SqlParameter("@PageNumber", pagination1.CurrentIndex);
        pa[3] = new SqlParameter("@PageSize", pagination1.PageSize);
		if (sql_where.Trim().Equals(""))
        {
            pa[4] = new SqlParameter("@Filter"," A.kich_hoat=1 ");
        }
        else
        {
			pa[4] = new SqlParameter("@Filter"," A.kich_hoat=1 and " + sql_where);
        }
        
        pa[5] = new SqlParameter("@Fields", "A.ma_ho_so, A.tieu_de_ho_so, A.so_luong_tuyen, A.luong_tu, A.luong_den, A.mo_ta, A.luong_tu, A.luong_den, convert(varchar, A.ngay_dang, 103) as ngay_dang_vn ");
        pa[6] = new SqlParameter("@Sort", "A.ngay_dang desc");
        DataTable dt = _common.getDataTable(CommandType.StoredProcedure, "dbo.Paging_SubQuery", pa);
        rpt_list.DataSource = dt;
        rpt_list.DataBind();
    }
    protected void pagination1_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndx = Convert.ToInt32(e.CommandArgument);
        pagination1.CurrentIndex = currnetPageIndx;
        if (_type.Trim().Equals(""))
        {
            LoadTuyendung();
        }
        else
        {
            if (_type.Trim().Equals("1"))
            {
                LoadTuyendungByNhatuyendung(_id);
            }
            if (_type.Trim().Equals("2"))
            {
                LoadTuyendungByNganhtuyendung(_id);
            }
            if (_type.Trim().Equals("3"))
            {
                LoadTuyendungByChucdanh(_id);
            }
        }
    }
    void LoadDoanhnghiep()
    {
        string _sql_cd = "select ma_doanh_nghiep as Ma, ten_doanh_nghiep as TEN from DoanhNghiep_doanh_nghiep where ma_doanh_nghiep IN (select ma_doanh_nghiep from ViecLamTrong_ho_so_tuyen_dung where kich_hoat = 1)";
        DataSet _ds = _common.ExeceuteDataset(_sql_cd);
        rpt_list_by.DataSource = _ds;
        rpt_list_by.DataBind();
        if (Request.Params["id"] == null)
        {
            if (_ds.Tables[0].Rows.Count > 0)
            {
                _id = _ds.Tables[0].Rows[0][0].ToString();
                LoadTuyendungByNhatuyendung(_id);
            }
        }
    }
    void LoadNganhtuyendung()
    {
        string _sql_cd = "select ma_chuyen_mon as MA, ten_chuyen_mon as TEN from DanhMuc_chuyen_mon where ma_chuyen_mon IN (select ma_chuyen_mon from ViecLamTrong_yeu_cau_chuyen_mon where kich_hoat = 1 group by ma_chuyen_mon)";
        DataSet _ds = _common.ExeceuteDataset(_sql_cd);
        rpt_list_by.DataSource = _ds;
        rpt_list_by.DataBind();
        if (Request.Params["id"] == null)
        {
            if (_ds.Tables[0].Rows.Count > 0)
            {
                _id = _ds.Tables[0].Rows[0][0].ToString();
                LoadTuyendungByNganhtuyendung(_id);
            }
        }
    }
    void LoadChucdanhTuyendung()
    {
        string _sql_cd = "select ma_chuc_danh as MA, ten_chuc_danh as TEN from DanhMuc_chuc_danh where ma_chuc_danh IN (select ma_chuc_danh from ViecLamTrong_ho_so_tuyen_dung where kich_hoat = 1 group by ma_chuc_danh)";
        DataSet _ds = _common.ExeceuteDataset(_sql_cd);
        rpt_list_by.DataSource = _ds;
        rpt_list_by.DataBind();
        if (Request.Params["id"] == null)
        {
            if (_ds.Tables[0].Rows.Count > 0)
            {
                _id = _ds.Tables[0].Rows[0][0].ToString();
                LoadTuyendungByChucdanh(_id);
            }
        }
    }
    void LoadTuyendungByNhatuyendung(string ma_doanh_nghiep)
    {
        DataTable totalrow = null;
		if (sql_where.Trim().Equals(""))
        {
            totalrow = _common.getDataTable("select ma_ho_so from ViecLamTrong_ho_so_tuyen_dung as A inner join doanhnghiep_doanh_nghiep as B on A.ma_doanh_nghiep = B.ma_doanh_nghiep where A.kich_hoat=1 and A.ma_doanh_nghiep = '" + ma_doanh_nghiep + "'");
        }
        else
        {
			totalrow = _common.getDataTable("select ma_ho_so from ViecLamTrong_ho_so_tuyen_dung as A inner join doanhnghiep_doanh_nghiep as B on A.ma_doanh_nghiep = B.ma_doanh_nghiep where A.kich_hoat=1 and A.ma_doanh_nghiep = '" + ma_doanh_nghiep + "' and "+sql_where);
			
        }

        pagination1.ItemCount = totalrow.Rows.Count;
        lbl_total.Text = totalrow.Rows.Count.ToString()+ " tuyển dụng thuộc doanh nghiệp: "+comm.execScalar("select ten_doanh_nghiep from DoanhNghiep_doanh_nghiep where ma_doanh_nghiep='"+ma_doanh_nghiep+"'");
        SqlParameter[] pa = new SqlParameter[7];

        pa[0] = new SqlParameter("@Tables", " ViecLamTrong_ho_so_tuyen_dung as A inner join doanhnghiep_doanh_nghiep as B on A.ma_doanh_nghiep = B.ma_doanh_nghiep  ");
        pa[1] = new SqlParameter("@PK", "A.ngay_dang");
        pa[2] = new SqlParameter("@PageNumber", pagination1.CurrentIndex);
        pa[3] = new SqlParameter("@PageSize", pagination1.PageSize);
		if (sql_where.Trim().Equals(""))
        {
            pa[4] = new SqlParameter("@Filter", " A.kich_hoat=1 and A.ma_doanh_nghiep = '" + ma_doanh_nghiep + "'");
        }
        else
        {
			pa[4] = new SqlParameter("@Filter", " A.kich_hoat=1 and A.ma_doanh_nghiep = '" + ma_doanh_nghiep + "' and "+sql_where);	
        }
        
        pa[5] = new SqlParameter("@Fields", " A.ngay_dang, A.ma_ho_so, A.tieu_de_ho_so, A.so_luong_tuyen, A.mo_ta, A.luong_tu, A.luong_den, convert(varchar, A.ngay_dang, 103) as ngay_dang_vn ");
		pa[6] = new SqlParameter("@Sort", "A.ngay_dang desc");
        DataTable dt = _common.getDataTable(CommandType.StoredProcedure, "dbo.Paging_SubQuery", pa);
        rpt_list.DataSource = dt;
        rpt_list.DataBind();
    }

    void LoadTuyendungByNganhtuyendung(string ma_chuyen_mon)
    {
        DataTable totalrow = null;
		if (sql_where.Trim().Equals(""))
        {
            totalrow = _common.getDataTable("select A.ma_ho_so from ViecLamTrong_ho_so_tuyen_dung as A inner join ViecLamTrong_yeu_cau_chuyen_mon as B on A.ma_ho_so = B.ma_ho_so where A.kich_hoat=1 and B.ma_chuyen_mon = '" + ma_chuyen_mon + "'");
        }
        else
        {
			totalrow = _common.getDataTable("select A.ma_ho_so from ViecLamTrong_ho_so_tuyen_dung as A inner join ViecLamTrong_yeu_cau_chuyen_mon as B on A.ma_ho_so = B.ma_ho_so where A.kich_hoat=1 and B.ma_chuyen_mon = '" + ma_chuyen_mon + "' and "+sql_where);			
        }
        

        pagination1.ItemCount = totalrow.Rows.Count;
        lbl_total.Text = totalrow.Rows.Count.ToString()+" tuyển dụng thuộc ngành: "+comm.execScalar("select ten_chuyen_mon from DanhMuc_chuyen_mon where ma_chuyen_mon='"+ma_chuyen_mon+"'");
        SqlParameter[] pa = new SqlParameter[7];

        pa[0] = new SqlParameter("@Tables", "  ViecLamTrong_ho_so_tuyen_dung as A inner join ViecLamTrong_yeu_cau_chuyen_mon as B on A.ma_ho_so = B.ma_ho_so ");
        pa[1] = new SqlParameter("@PK", "A.ngay_dang");
        pa[2] = new SqlParameter("@PageNumber", pagination1.CurrentIndex);
        pa[3] = new SqlParameter("@PageSize", pagination1.PageSize);
		if (sql_where.Trim().Equals(""))
        {
            pa[4] = new SqlParameter("@Filter", " A.kich_hoat=1 and B.ma_chuyen_mon = '" + ma_chuyen_mon + "'");
        }
        else
        {
			pa[4] = new SqlParameter("@Filter", " A.kich_hoat=1 and B.ma_chuyen_mon = '" + ma_chuyen_mon + "' and "+sql_where);	
        }
        
        pa[5] = new SqlParameter("@Fields", " A.ma_ho_so, A.tieu_de_ho_so, A.so_luong_tuyen, A.luong_tu, A.luong_den, A.mo_ta, A.luong_tu, A.luong_den, convert(varchar, A.ngay_dang, 103) as ngay_dang_vn ");
		pa[6] = new SqlParameter("@Sort", "A.ngay_dang desc");
        DataTable dt = _common.getDataTable(CommandType.StoredProcedure, "dbo.Paging_SubQuery", pa);
        rpt_list.DataSource = dt;
        rpt_list.DataBind();
    }

    void LoadTuyendungByChucdanh(string ma_chuc_danh)
    {
        DataTable totalrow = null;
		if (sql_where.Trim().Equals(""))
        {
            totalrow = _common.getDataTable("select A.ma_ho_so from ViecLamTrong_ho_so_tuyen_dung as A where A.kich_hoat=1 and A.ma_chuc_danh = '" + ma_chuc_danh + "'");
        }
        else
        {
			totalrow = _common.getDataTable("select A.ma_ho_so from ViecLamTrong_ho_so_tuyen_dung as A where A.kich_hoat=1 and A.ma_chuc_danh = '" + ma_chuc_danh + "' and "+sql_where);
        }

        pagination1.ItemCount = totalrow.Rows.Count;
        lbl_total.Text = totalrow.Rows.Count.ToString()+" tuyển dụng tuyển chức danh: "+comm.execScalar("select ten_chuc_danh from DanhMuc_chuc_danh where ma_chuc_danh='"+ma_chuc_danh+"'");
        SqlParameter[] pa = new SqlParameter[7];

        pa[0] = new SqlParameter("@Tables", "  ViecLamTrong_ho_so_tuyen_dung as A");
        pa[1] = new SqlParameter("@PK", "A.ngay_dang");
        pa[2] = new SqlParameter("@PageNumber", pagination1.CurrentIndex);
        pa[3] = new SqlParameter("@PageSize", pagination1.PageSize);
		if (sql_where.Trim().Equals(""))
        {
            pa[4] = new SqlParameter("@Filter", " A.kich_hoat=1 and A.ma_chuc_danh = '" + ma_chuc_danh + "'");
        }
        else
        {
			pa[4] = new SqlParameter("@Filter", " A.kich_hoat=1 and A.ma_chuc_danh = '" + ma_chuc_danh + "' and "+sql_where);	
        }
        
        pa[5] = new SqlParameter("@Fields", " A.ma_ho_so, A.tieu_de_ho_so, A.so_luong_tuyen, A.luong_tu, A.luong_den, A.mo_ta, A.luong_tu, A.luong_den, convert(varchar, A.ngay_dang, 103) as ngay_dang_vn ");
		pa[6] = new SqlParameter("@Sort", "A.ngay_dang desc");
        DataTable dt = _common.getDataTable(CommandType.StoredProcedure, "dbo.Paging_SubQuery", pa);
        rpt_list.DataSource = dt;
        rpt_list.DataBind();
    }


    protected void btn_src_Click(object sender, EventArgs e)
    {
        //string _sql_src = "";
		sql_where="";
        if (radio_src_today.Checked)
        {
            sql_where = " convert(varchar, A.ngay_dang, 103) = '" + DateTime.Now.ToString("dd/MM/yyyy") + "' ";
        }
        if (radio_src_thisweek.Checked)
        {
            string[] _dayinweek = new string[7];
            _dayinweek[0] = DayOfWeek.Sunday.ToString();
            _dayinweek[1] = DayOfWeek.Monday.ToString();
            _dayinweek[2] = DayOfWeek.Tuesday.ToString();
            _dayinweek[3] = DayOfWeek.Wednesday.ToString();
            _dayinweek[4] = DayOfWeek.Thursday.ToString();
            _dayinweek[5] = DayOfWeek.Friday.ToString();
            _dayinweek[6] = DayOfWeek.Saturday.ToString();
            int _vitri_ngay_truoc = 0;
            int _vitri_ngay_sau = 0;
            for (int i = 1; i <= _dayinweek.Length; i++)
            {
                if (_dayinweek[i - 1].Equals(DateTime.Now.DayOfWeek.ToString()))
                {
                    _vitri_ngay_truoc = i;
                    _vitri_ngay_sau = 7 - i;
                }
            }
            sql_where = " A.ngay_dang between '" + DateTime.Now.AddDays(-_vitri_ngay_truoc+1) + "' and '" + DateTime.Now.AddDays(_vitri_ngay_sau) + "' ";
        }
        if (radio_src_thismonth.Checked)
        {
            sql_where = " month(A.ngay_dang) = month(getdate()) and year(A.ngay_dang) = year(getdate()) ";
        }
        if (radio_src_range.Checked)
        {
            if (txt_src_to_RadDatePicker.SelectedDate.HasValue && txt_src_from_RadDatePicker.SelectedDate.HasValue)
            {
                sql_where = " A.ngay_dang between '" + txt_src_from_RadDatePicker.SelectedDate.Value + "' and '" + txt_src_to_RadDatePicker.SelectedDate.Value + "' ";
            }
        }
        if (_type.Trim().Equals(""))
        {
            LoadTuyendung();
        }
        else
        {
            if (_type.Trim().Equals("1"))
            {
                LoadTuyendungByNhatuyendung(_id);
            }
            if (_type.Trim().Equals("2"))
            {
                LoadTuyendungByNganhtuyendung(_id);
            }
            if (_type.Trim().Equals("3"))
            {
                LoadTuyendungByChucdanh(_id);
            }
        }
    }

    protected void rpt_list_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Label _lbl_luong = e.Item.FindControl("lbl_luong") as Label;
            DataRowView r = (DataRowView)e.Item.DataItem;
            _lbl_luong.Text = GetLuong(r["luong_tu"].ToString(), r["luong_den"].ToString());
        }
    }
    string GetLuong(string luong_tu, string luong_den)
    {
        string _luong = "";
        if (luong_tu.Trim().Equals("0") && luong_den.Trim().Equals("0"))
        {
            _luong = "Thoả thuận";
        }
        else
        {
            _luong = "Từ " + luong_tu + " đến " + luong_den;
        }
        return _luong;
    }
}
